package com.study;

import com.study.job.PrintWordsJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class QuartzDemo {

    public static void main(String[] args) throws SchedulerException, InterruptedException {
// 1、创建调度器Scheduler
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        // 2、创建JobDetail实例，并与PrintWordsJob类绑定(Job执行内容)
        JobDetail jobDetail = JobBuilder.newJob(PrintWordsJob.class)
                .withIdentity("job1", "group1").build();
        // 3、构建Trigger实例,每隔1s执行一次
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1")
                .startNow()//立即生效
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(1)//每隔1s执行一次
                        .repeatForever()).build();//一直执行

        //4、执行
        scheduler.scheduleJob(jobDetail, trigger);
        System.out.println("--------scheduler start ! ------------");
        scheduler.start();

        //睡眠
        TimeUnit.MINUTES.sleep(1);
        scheduler.shutdown();
        System.out.println("--------scheduler shutdown ! ------------");
    }
}

/*
第一次接触Quartz的任务调度，把Job的实现类和调度的程序放在了一个java文件里但是却出现了以下的错误：


org.quartz.SchedulerException: Problem instantiating class 'com.jiaoyiping.stuty_quartz.SimpleJob2' [See nested exception: java.lang.IllegalAccessException: Class org.quartz.simpl.SimpleJobFactory can not access a member of class com.jiaoyiping.stuty_quartz.SimpleJob2 with modifiers ""]
 at org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:58)
 at org.quartz.simpl.PropertySettingJobFactory.newJob(PropertySettingJobFactory.java:69)
 at org.quartz.core.JobRunShell.initialize(JobRunShell.java:134)
 at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:387)
Caused by: java.lang.IllegalAccessException: Class org.quartz.simpl.SimpleJobFactory can not access a member of class com.jiaoyiping.stuty_quartz.SimpleJob2 with modifiers ""
 at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
 at java.lang.Class.newInstance0(Class.java:368)
 at java.lang.Class.newInstance(Class.java:327)
 at org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:56)
 ... 3 more

意思是反射的时候出错，不能实例化Job

网上查了一下，发现在反射的时候，Job的实现类需要定义成Public的，否则就无法实例化，所以把实现类和调度程序放在同一个.java文件夹下是不可行的

把Job实现类放在一个独立的java文件里，然后把class定义成public的，然后问题就解决了。
 */